package com.fcat.base.common.util;

//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;

import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;

public class Convert {
//	private final static Logger log = LoggerFactory.getLogger(Convert.class);
	public final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public final static SimpleDateFormat ymdhms = new SimpleDateFormat("yyMMddHHmmss");
	public final static SimpleDateFormat yymdhms = new SimpleDateFormat("yyyyMMddHHmmss");
	public final static SimpleDateFormat ddMMyyyyHHmmss = new SimpleDateFormat("ddMMyyyyHHmmss");
	/**
	 * 将十六进制转化为二进制代码,同时高位补0,补<code>offset</code>个<br>
	 * 例：参数：00，结果：00000000
	 * 
	 * @param strHex
	 *            String 十六进制
	 * @param offset
	 *            int 补0个数
	 * @return String
	 */
	public static String hex2Binary(String strHex, int offset) {
		if (offset != 0) {
			int i = Integer.parseInt(strHex, 16);
			strHex = Integer.toBinaryString(i);
			if (strHex.length() * 4 == offset) {
				return strHex;
			}
			int l = offset - strHex.length();
			char[] c = new char[offset];
			for (int j = 0; j < l; j++) {
				c[j] = '0';
			}
			char[] c2 = strHex.toCharArray();
			for (int j = 0; j < c2.length; j++) {
				c[j + l] = c2[j];
			}
			strHex = String.valueOf(c);
		}
		return strHex;
	}

	/**
	 * 将十六进制转化为二进制代码,同时高位补0(补满八位)
	 * 
	 * @param strHex
	 *            String 十六进制
	 * @return String
	 */
	public static String hex2Binary(String strHex) {
		String binaryCode = "";
		String zero = "00000000";
		long tenCode = Long.parseLong(strHex, 16);
		binaryCode = Long.toBinaryString(tenCode);
		if (binaryCode.length() < 8) {
			binaryCode = zero.substring(0, 8 - binaryCode.length())
					+ binaryCode;
		}
		return binaryCode;
	}

	/**
	 * 十六进制转GBK
	 * 
	 * @param bytes
	 *            String 十六进制字符串
	 * @return String
	 */
	public static String mixHex2str(String bytes) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream(
				bytes.length() / 2);
		String opr = null;
		for (int i = 0; i < bytes.length(); i += 2) {
			if (bytes.charAt(i) == '0' && bytes.charAt(i + 1) == '0')
				break;
			baos.write(("0123456789ABCDEF".indexOf(bytes.charAt(i)) << 4 | "0123456789ABCDEF"
					.indexOf(bytes.charAt(i + 1))));
		}
		try {
			opr = new String(baos.toByteArray(), "GBK");
		} catch (Exception e) {
//			log.error("Exception", e);
			e.printStackTrace();
		}
		if (bytes.charAt(0) == '0' && bytes.charAt(1) == '0')
			opr = "";
		return opr;
	}

	/**
	 * 字符串转GBK
	 * 
	 * @param str
	 * @return
	 */
	public static String mixStr2Hex(String str) {
		byte[] bytes = null;
		try {
			bytes = str.getBytes("GBK");
		} catch (UnsupportedEncodingException e) {
//			log.error("UnsupportedEncodingException", e);
			e.printStackTrace();
		}
		StringBuilder sb = new StringBuilder(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++) {
			sb.append("0123456789ABCDEF".charAt((bytes[i] & 0xf0) >> 4));
			sb.append("0123456789ABCDEF".charAt((bytes[i] & 0x0f) >> 0));
		}
		return sb.toString();
	}

	/**
	 * 十六进制转UTF8
	 * 
	 * @param bytes
	 *            String 十六进制字符串
	 * @return String
	 */
	public static String mixHex2strCS(String bytes) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream(
				bytes.length() / 2);
		String opr = null;
		for (int i = 0; i < bytes.length(); i += 2) {
			if (bytes.charAt(i) == '0' && bytes.charAt(i + 1) == '0')
				break;
			baos.write(("0123456789ABCDEF".indexOf(bytes.charAt(i)) << 4 | "0123456789ABCDEF"
					.indexOf(bytes.charAt(i + 1))));
		}
		try {
			opr = new String(baos.toByteArray(), "utf-8");// ISO-8859-1 utf-8
		} catch (Exception e) {
//			log.error("Exception", e);
			e.printStackTrace();
		}
		if (bytes.charAt(0) == '0' && bytes.charAt(1) == '0')
			opr = "";
		return opr;
	}

	public static String mixStr2Hex1CS(String str) {
		byte[] bytes = null;
		try {
			bytes = str.getBytes("UTF-8");// UTF-8
		} catch (UnsupportedEncodingException e) {
//			log.error("UnsupportedEncodingException", e);
			e.printStackTrace();
		}
		StringBuilder sb = new StringBuilder(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++) {
			sb.append("0123456789ABCDEF".charAt((bytes[i] & 0xf0) >> 4));
			sb.append("0123456789ABCDEF".charAt((bytes[i] & 0x0f) >> 0));
		}
		return sb.toString();
	}

	public static String byteHEX(byte ib) {
		char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
				'B', 'C', 'D', 'E', 'F' };
		char[] ob = new char[2];
		ob[0] = Digit[(ib >>> 4) & 0X0F];
		ob[1] = Digit[ib & 0X0F];
		String s = new String(ob);
		return s;
	}

	public static byte[] MyNumLong2ByteArray(long Number, int length) {
		String zeroStr = "";
		for (int i = 0; i < length; i++) {
			zeroStr += "0";
		}
		String str_byte = Long.toHexString(Number);
		byte[] return_byte = str2Bcd(zeroStr.substring(0,
				length - str_byte.length())
				+ str_byte);
		return return_byte;
	}

	public static byte[] str2Bcd(String asc) {
		int len = asc.length();
		int mod = len % 2;

		if (mod != 0) {
			asc = "0" + asc;
			len = asc.length();
		}

		byte abt[] = new byte[len];
		if (len >= 2) {
			len = len / 2;
		}

		byte bbt[] = new byte[len];
		abt = asc.getBytes();
		int j, k;

		for (int p = 0; p < asc.length() / 2; p++) {
			if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
				j = abt[2 * p] - '0';
			} else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
				j = abt[2 * p] - 'a' + 0x0a;
			} else {
				j = abt[2 * p] - 'A' + 0x0a;
			}

			if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
				k = abt[2 * p + 1] - '0';
			} else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
				k = abt[2 * p + 1] - 'a' + 0x0a;
			} else {
				k = abt[2 * p + 1] - 'A' + 0x0a;
			}

			int a = (j << 4) + k;
			byte b = (byte) a;
			bbt[p] = b;
		}
		return bbt;
	}

	public static byte binary2Hex(String str) {
		byte reByte = 0;
		int tenNum = Integer.parseInt(str, 2);// 十进制表示
		reByte = str2byteOne(tenNum + "");
		return reByte;
	}

	public static byte str2byteOne(String number) {
		byte retByte = 0;
		retByte = str2BcdMe(Integer.toHexString(Integer.valueOf(number)))[0];
		return retByte;
	}

	public static byte[] str2BcdMe(String asc) {// 正确
		int len = asc.length();
		int mod = len % 2;
		if (mod != 0) {
			asc = "0" + asc;
			len = asc.length();
		}
		byte abt[] = new byte[len];
		if (len >= 2) {
			len = len / 2;
		}
		byte bbt[] = new byte[len];
		try {
			abt = asc.getBytes("GBK");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
//			log.error("UnsupportedEncodingException", e);
			e.printStackTrace();
		}
		int j, k;
		for (int p = 0; p < asc.length() / 2; p++) {
			if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
				j = abt[2 * p] - '0';
			} else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
				j = abt[2 * p] - 'a' + 0x0a;
			} else {
				j = abt[2 * p] - 'A' + 0x0a;
			}
			if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
				k = abt[2 * p + 1] - '0';
			} else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
				k = abt[2 * p + 1] - 'a' + 0x0a;
			} else {
				k = abt[2 * p + 1] - 'A' + 0x0a;
			}
			int a = (j << 4) + k;
			byte b = (byte) a;
			bbt[p] = b;
		}
		return bbt;
	}

	public static byte[] arraycat(byte[] buf1, byte[] buf2) {
		byte[] bufret = null;
		int len1 = 0;
		int len2 = 0;
		if (buf1 != null)
			len1 = buf1.length;
		if (buf2 != null)
			len2 = buf2.length;
		if (len1 + len2 > 0)
			bufret = new byte[len1 + len2];
		if (len1 > 0)
			System.arraycopy(buf1, 0, bufret, 0, len1);
		if (len2 > 0)
			System.arraycopy(buf2, 0, bufret, len1, len2);
		return bufret;
	}

	// 交通部车机终端（北京） 字符转义
	public static byte[] replaceCarByte(byte[] srcByte) {
		String src = "";
		for (int i = 0; i < srcByte.length; i++) {
			String s = byteHEX(srcByte[i]);
			if (s.equals("7D")) {
				s = s.replaceAll("7D", "7D01");
				src += s;
				continue;
			}
			if (s.equals("7E")) {
				s = s.replaceAll("7E", "7D02");
				src += s;
				continue;
			}
			src += s;
		}
		byte[] targetByte = str2Bcd(src);
		return targetByte;
	}

	/**
	 * 整形车word
	 * 
	 * @param Number
	 * @return
	 */
	public static byte[] word(long Number) {
		String zeroStr = "0000";
		String str_byte = Long.toHexString(Number);
		byte[] return_byte = str2Bcd(zeroStr
				.substring(0, 4 - str_byte.length()) + str_byte);
		return return_byte;
	}

	public static byte[] dWord(long Number) {
		String zeroStr = "00000000";
		String str_byte = Long.toHexString(Number);
		byte[] return_byte = str2Bcd(zeroStr
				.substring(0, 8 - str_byte.length()) + str_byte);
		return return_byte;
	}


	public static byte[] byteJTS(String NumberStr) {
		int Number = Integer.parseInt(NumberStr);

		String str_byte = Integer.toHexString(Number);
		if (str_byte.length() % 2 != 0) {
			str_byte = "0" + str_byte;
		}
		byte[] return_byte = str2Bcd(str_byte);
		return return_byte;
	}

	// byteJT("1", 1); 结果为：0x01
	public static byte[] byteJT(String NumberStr, int length) {
		int Number = Integer.parseInt(NumberStr);
		String zeroStr = "";
		for (int i = 0; i < length; i++) {
			zeroStr += "00";
		}
		String str_byte = Integer.toHexString(Number);
		byte[] return_byte = str2Bcd(zeroStr.substring(0,
				length * 2 - str_byte.length())
				+ str_byte);
		return return_byte;
	}

	public static byte[] octetString2(String str) {
		byte[] rspByte = str2Bcd(mixStr2HexLength2(str));
		return rspByte;
	}

	public static String mixStr2HexLength2(String str) {
		byte[] bytes = null;
		try {
			bytes = str.getBytes("GBK");// ISO-8859-1 GBK
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
//			log.error("UnsupportedEncodingException", e1);
			e1.printStackTrace();
		}
		StringBuilder sb = new StringBuilder(bytes.length * 2);
		for (int i = 0; i < bytes.length; i++) {
			sb.append("0123456789ABCDEF".charAt((bytes[i] & 0xf0) >> 4));
			sb.append("0123456789ABCDEF".charAt((bytes[i] & 0x0f) >> 0));
		}
		String strHex = sb.toString();
		return strHex;
	}

	public static byte[] JW2HEX_JTB(float f) {
		int k = (int) (f * 1000000);
		byte[] value = dWord(k);
		return value;
	}

	/**
	 *  16进制字符串转换为byte数组
	 * @param
	 * @return byte[]
	 */
	public static byte[] hexStringToByte(String hex) {
		int len = (hex.length() / 2);
		byte[] result = new byte[len];
		char[] achar = hex.toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		}
		return result;
	}

	private static byte toByte(char c) {
		byte b = (byte) "0123456789ABCDEF".indexOf(c);
		return b;
	}

	/**
	 * @param bArray
	 * @return
	 */
	public static final String bytesToHexString(byte[] bArray) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		for (int i = 0; i < bArray.length; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}

	/**
	 * 16进制字符串转换为BCD码表示的byte[].
	 * 
	 * @param hexStr
	 * @return byte[]
	 */
	public static byte[] HexStrToByteArray(String hexStr) {
		int len = hexStr.length();
		int mod = len % 2;

		if (mod != 0) {
			hexStr = "0" + hexStr;
			len = hexStr.length();
		}

		byte abt[] = new byte[len];
		if (len >= 2) {
			len = len / 2;
		}

		byte bbt[] = new byte[len];
		abt = hexStr.getBytes();
		int j, k;

		for (int p = 0; p < hexStr.length() / 2; p++) {
			if ((abt[2 * p] >= '0') && (abt[2 * p] <= '9')) {
				j = abt[2 * p] - '0';
			} else if ((abt[2 * p] >= 'a') && (abt[2 * p] <= 'z')) {
				j = abt[2 * p] - 'a' + 0x0a;
			} else {
				j = abt[2 * p] - 'A' + 0x0a;
			}

			if ((abt[2 * p + 1] >= '0') && (abt[2 * p + 1] <= '9')) {
				k = abt[2 * p + 1] - '0';
			} else if ((abt[2 * p + 1] >= 'a') && (abt[2 * p + 1] <= 'z')) {
				k = abt[2 * p + 1] - 'a' + 0x0a;
			} else {
				k = abt[2 * p + 1] - 'A' + 0x0a;
			}

			int a = (j << 4) + k;
			byte b = (byte) a;
			bbt[p] = b;
		}
		return bbt;
	}

	/**
	 * 填充0
	 * 
	 * @param text
	 *            {@link String} 需要补0的字符
	 * @param length
	 *            {@link Integer} 补0后的长度
	 * @return {@link String}
	 */
	public static String fillZero(String text, int length) {
		if (text == null) {
			for (int i = 0; i < length; i++)
				text = "0" + text;

		} else {
			for (int i = text.length(); i < length; i++) {
				text = "0" + text;
			}
		}
		return text;
	}
	/**
	 * 填充0
	 * 
	 * @param
	 *            {@link Long} 需要补0的数字
	 * @param length
	 *            {@link Integer} 补0后的长度
	 * @return {@link String}
	 */
	public static String fillZero(long number, int length) {
		String text = String.valueOf(number);
		for (int i = text.length(); i < length; i++) {
			text = "0" + text;
		}
		return text;
	}

	/**
	 * 整形字符串转十六进制
	 * 
	 * @param value
	 *            {@link String} 整形字符串
	 * @param length
	 *            补0后长度
	 * @return {@link String}
	 */
	public static String longStrToHexString(String value, int length) {
		long l = Long.parseLong(value);
		String hex = Long.toHexString(l).toUpperCase();
		return fillZero(hex, length);
	}

	/**
	 * 十进制转十六进制
	 * 
	 * @param number
	 *            int 十进制
	 * @param x
	 *            int 位数
	 * @return
	 */
	public static String decimalToHexadecimal(long number, int x) {
		String hex = Long.toHexString(number).toUpperCase();
		return fillZero(hex, x);
	}
	
	/**
	 * long to int
	 */
	public static int long2int(Long L) {
		return L.intValue();
	}

	 /**
     * 将16进制字符串转换为byte[]
     * 
     * @param str
     * @return
     */
    public static byte[] toBytes(String str) {
        if(str == null || str.trim().equals("")) {
            return new byte[0];
        }

        byte[] bytes = new byte[str.length() / 2];
        for(int i = 0; i < str.length() / 2; i++) {
            String subStr = str.substring(i * 2, i * 2 + 2);
            bytes[i] = (byte) Integer.parseInt(subStr, 16);
        }

        return bytes;
    }
    
	public static void main(String[] args){
		//System.out.println(hexStringToString("01C900010001010000000000000000000000000000000000000000000000006A000108010C0008010C010C0001010C000001080100080C000C00080101080C000C00080100010C0008010C000C010C000001080100010C0000010C0001000C000001080101000C0008010C0001010C0001000C0008000C000800080101010C000801080108000C0001000C0001000C0008000C000101080100010C000C010C000800080001080801010C080101010801010808010800080008080C000800080001080C0008010C0008000C0000080C000C010C000C010C0000000C000C000C0000000C000C010C000C000C000C010C0000000C0000010C0000010C0008000800000C080008000800000108010101080108010C01010808010C00080101010801010C00010C0C080100010801010108010800080008080C0008000800010C00010001010108010C0000000C000008010108000800000C08000800080000010C0101080801010C0801010108010800080008080C000800080001080C0101080C0101080C0101080C01010C0800010C0801010C0801010C0800000108010001080100000800000800010008000108080C00010C08010108080108080C000C000C010C000C0108000800AA"));
	}
}